﻿using MediatR;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using RBAC_Uthorization.Read.API.Application.Command.UserInfoCommand;
using RBAC_Uthorization.Read.API.DTO;
using RBAC_Uthorization.Read.API.DTO.Request;
using RBAC_Uthorization.Read.API.DTO.Response.User;
using System.IdentityModel.Tokens.Jwt;
using System.Numerics;
using System.Security.Claims;


namespace RBAC_Uthorization.Read.API.Controllers
{
    [Route("api/[controller]/[action]")]
    [Authorize]
    [ApiController]
    public class UserController : ControllerBase
    {
        private readonly IMediator _mediator;
        private readonly ILogger<UserController> _logger;

        public UserController(IMediator mediator, ILogger<UserController> logger)
        {
            _mediator = mediator;
            _logger = logger;
        }
        /// <summary>
        /// 用户显示
        /// </summary>
        /// <returns></returns>
        [HttpGet]
        public async Task<ApiResult<PagingDto>> userShow(string? phone, int login_flag,string? name,string? no,int pageSize=10,int pageIndex=1)
        {
            try
            {
                var entity = await _mediator.Send(new UserInfoShowCommand() {
                    offlc_id=HttpContext.User.FindFirst(JwtRegisteredClaimNames.Sid)?.Value,
                    phone= phone, 
                    name=name,
                    no=no,
                    pageSize= pageSize,
                    pageIndex= pageIndex, 
                    login_flag=login_flag,
                    sys_id = HttpContext.User.FindFirst(JwtRegisteredClaimNames.Jti)?.Value

                });
                return entity;
            }
            catch (Exception ex)
            {
                _logger.LogError(ex, "用户查询异常");
                throw;
            }
        }
        /// <summary>
        /// 用户select显示
        /// </summary>
        /// <returns></returns>
        [HttpGet]
        public async Task<ApiResult<List<UserSelectShow>>> UserSelectShow()
        {
            var entity = await _mediator.Send(new UserSelectShowCommand() { 
                offic_id =HttpContext.User.FindFirst(JwtRegisteredClaimNames.Sid)?.Value,
                sys_id = HttpContext.User.FindFirst(JwtRegisteredClaimNames.Jti)?.Value
            });
            return entity;
        }
        /// <summary>
        /// 用户详情
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        [HttpGet]
        public async Task<ApiResult<UserFindIdDto>> UserFindId(int id)
        {
            var entity = await _mediator.Send(new UserFindIdCommand() { 
                id = id,
                sys_id = HttpContext.User.FindFirst(JwtRegisteredClaimNames.Jti)?.Value
            });
            return entity;
        }

    }
}
